#!/bin/bash
# CONTENT: this shell runs ALL scripts in ARE INTERMEDIARY CONSTRAINTS PRICED? by Du, Hebert, Huber in RFS 2022
# OPTIONAL: run this file with an argument, which will be the path to the replication directory, otherwise uses default path
# REMEMBER: to set permission with chmod 755 <script> OR chmod +x <script>
# NOTE: log files will be automatically generated by Stata.  All but error messages are suppressed right now.

if [ -z "$1" ]
  then
	PATHVAR="/Users/amyhuber/Dropbox/ForwardArbitrage/CodeReplication/"
else
	PATHVAR=$1
fi

#---------------------------------------------------------
# Section 0: Install requisite packages
#---------------------------------------------------------

## R
CODEVAR="${PATHVAR}RCode/0_PackageConfig.R"
nohup Rscript "$CODEVAR" $PATHVAR

## STATA
CODEVAR="${PATHVAR}StataCode/config_stata.do"
nohup /Applications/Stata/StataMP.app/Contents/MacOS/stata-mp -b run "$CODEVAR"

#---------------------------------------------------------
# Section 1: Forward CIP Trading Strategy Returns
#---------------------------------------------------------

## Clean data from raw files conforming to templates SkeletonData
CODEVAR="${PATHVAR}RCode/1_CleanData.R"
nohup Rscript "$CODEVAR" $PATHVAR

## Construct CIP basis, forward CIP trading strategy profits
CODEVAR="${PATHVAR}RCode/2_BasisReturns.R"
nohup Rscript "$CODEVAR" $PATHVAR

## Construct portfolios of forward CIP trading strategy, including PC of near-arbitrages
CODEVAR="${PATHVAR}StataCode/port_embi.do"
nohup /Applications/Stata/StataMP.app/Contents/MacOS/stata-mp -b run "$CODEVAR"

CODEVAR="${PATHVAR}StataCode/short_basis.do"
nohup /Applications/Stata/StataMP.app/Contents/MacOS/stata-mp -b run "$CODEVAR"

CODEVAR="${PATHVAR}StataCode/spot_arbitrage.do"
nohup /Applications/Stata/StataMP.app/Contents/MacOS/stata-mp -b run "$CODEVAR"

CODEVAR="${PATHVAR}StataCode/PCAPortfolios.do"
nohup /Applications/Stata/StataMP.app/Contents/MacOS/stata-mp -b run "$CODEVAR"

CODEVAR="${PATHVAR}RCode/3_Portfolio.R"
nohup Rscript "$CODEVAR" $PATHVAR

## Compute summary statistics of forward CIP trading strategy profits
CODEVAR="${PATHVAR}RCode/4_SummaryStats.R"
nohup Rscript "$CODEVAR" $PATHVAR

#---------------------------------------------------------
# Section 2: Cross-sectional Asset Return Tests
#---------------------------------------------------------

## Construct panel of asset returns
CODEVAR="${PATHVAR}RCode/5a_Bond_CDS_Comm.R"
nohup Rscript "$CODEVAR" $PATHVAR

CODEVAR="${PATHVAR}RCode/5b_Option.R"
nohup Rscript "$CODEVAR" $PATHVAR

CODEVAR="${PATHVAR}StataCode/ReturnPredictability.do"
nohup /Applications/Stata/StataMP.app/Contents/MacOS/stata-mp -b run "$CODEVAR"

CODEVAR="${PATHVAR}StataCode/aem_hkm.do"
nohup /Applications/Stata/StataMP.app/Contents/MacOS/stata-mp -b run "$CODEVAR"

CODEVAR="${PATHVAR}StataCode/fig_arb_basis_aem.do"
nohup /Applications/Stata/StataMP.app/Contents/MacOS/stata-mp -b run "$CODEVAR"

CODEVAR="${PATHVAR}RCode/5_ReturnPanel.R"
nohup Rscript "$CODEVAR" $PATHVAR

CODEVAR="${PATHVAR}StataCode/MakeCrossSectionData.do"
nohup /Applications/Stata/StataMP.app/Contents/MacOS/stata-mp -b run "$CODEVAR"

## GMM of cross sectional asset returns
CODEVAR="${PATHVAR}StataCode/RunGMMStata.do"
nohup /Applications/Stata/StataMP.app/Contents/MacOS/stata-mp -b run "$CODEVAR"

#---------------------------------------------------------
# Section 3: Output Tables and Graphs for the Paper
#---------------------------------------------------------

## Output figures and tables
CODEVAR="${PATHVAR}RCode/7_Format.R"
nohup Rscript "$CODEVAR" $PATHVAR &

## Bayesian horse race
CODEVAR="${PATHVAR}RCode/8_Bayesian.R"
nohup Rscript "$CODEVAR" $PATHVAR &

#: <<'END'
#END
